let pairs = ['(', ')']
let n = 3
let result = []
//["((()))","(()())","(())()","()(())","()()()"]
function backtrack(current, left, right){
    if(current.length == 2 * n ){
        result.push(current.join(''))
        return
    }

    if(left < n){
        current.push('(')
        backtrack(current, left + 1, right)
        current.pop()
    }
    if(right < left){
        current.push(')')
        backtrack(current, left, right + 1)
        current.pop()
    }
}

backtrack([], 0, 0)


console.log(result)